# -*- coding: utf8 -*-

import string
from puppy.query import QueryFilter
from puppy.model import Query

class BlackListFilter(QueryFilter):
  """Removes inappropriate original query terms."""
  
  def __init__(self, options=""):
    """Constructor for BlackListFilter."""
    super(BlackListFilter, self).__init__(options)
    self.info = "Removes inappropiate original query terms with extra terms."
    self.black_list_string = " ".join(filter(str.isalpha, self.options['terms'].replace('+', ' ').lower().split()))
   
  def run(self, query):
    """Removes inappropriate original query terms.
    
    Extra terms are passed to the filter via its options['terms'] entry.  For convenience, the string returned from options['terms'] has '+' characters where there should be spaces.  This is resolved prior to query expansion.
    
    Parameters:
    
    * query (puppy.model.Opensearch.Query): original query
    
    Returns:
    
    * query (puppy.model.Opensearch.Query): filtered query
    
    """

    search_terms_list = query.search_terms.split()       
        
    for search_term in search_terms_list:
      search_term_filtered = "".join(filter(str.isalpha, list(search_term.lower())))
      if search_term_filtered in self.black_list_string:
        query.search_terms = query.search_terms.replace(search_term, '')
    
    """" Deleting extra blank spaces """
    search_terms_list = query.search_terms.split()
    query.search_terms = ' '.join(search_terms_list)
     
    return query
    